#include "global.h"

PictureViewer::PictureViewer(QString file_path): QWidget()
{
    this->file_path = file_path;

    center = new QLabel(this);
    setWindowFlags(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);

    pixmap = new QPixmap(file_path);

    if(file_path.endsWith("gif")){
        movie = new QMovie(file_path);
        center->setMovie(movie);
        movie->start();
    }else{
        center->setPixmap(*pixmap);
    }
    setGeometry(0, 0, pixmap->size().width(), pixmap->size().height());

}

void PictureViewer::mousePressEvent(QMouseEvent *event)
{
}

void PictureViewer::mouseReleaseEvent(QMouseEvent *event)
{
}

void PictureViewer::mouseMoveEvent(QMouseEvent *event)
{
    if(event->type() == QEvent::MouseMove){
        setGeometry(geometry().x() + event->x(), geometry().y() + event->y(),
                    size().width(), size().height());
    }
}

void PictureViewer::keyPressEvent(QKeyEvent *event)
{
    switch(event->key()){
        case Qt::Key_Escape:
            close();
            break;
        default:
            QWidget::keyPressEvent(event);
            break;
    }
}

PictureViewer::~PictureViewer()
{
    qDebug() << "~PictureViewer" << endl;
    if(movie) delete movie;
    if(pixmap) delete pixmap;
    if(center) delete center;

}
